//
//  ZYCTeachingTeachLearningDetailViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/28.
//

#import "ZYCTeachingTeachLearningDetailViewController.h"
#import "ZYCTeachingTeachLearnStatisticsViewController.h"
#import "ZYCTeachLearningDetailModel.h"
#import "ZYCTeachingTeachLearningDetailHeaderView.h"
#import "ZYCTeachingTeachLearningDetailCell.h"
#import "ZYCTeachingTeachLearningDetailContentCell.h"
#import "ZYCMessageRevokeView.h"

@interface ZYCTeachingTeachLearningDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIButton *rightButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCTeachingTeachLearningDetailHeaderView *headerView;
@property (nonatomic, strong) ZYCTeachLearningDetailModel *detailModel;
@property (nonatomic, strong) ZYCMessageRevokeView *revokeView;


@end

@implementation ZYCTeachingTeachLearningDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"详情";
    //发布状态（2：进行中；3：已结束）
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.tableView);
        make.width.mas_equalTo(kScreenWidth);
    }];
    [self.tableView layoutIfNeeded];
    //
    [self loadTeachLearningDetail];
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.detailModel.subjectVoList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCDetailSubjectModel *model = self.detailModel.subjectVoList[indexPath.section];
    if ([model.subjectType integerValue] == 3) {//简单题
        ZYCTeachingTeachLearningDetailContentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeachingTeachLearningDetailContentCell class])];
        cell.model = model;
        return cell;
    }else{//选择题 (单选/多选)
        ZYCTeachingTeachLearningDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTeachingTeachLearningDetailCell class]) forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.0f;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footer = [UIView new];
    footer.backgroundColor = HEX_RGB(@"#F9F9F9");
    return footer;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [UIView new];
    header.backgroundColor = HEX_RGB(@"#F9F9F9");
    return header;
}





#pragma mark - 网络请求
- (void)loadTeachLearningDetail{
    NSDictionary *params = @{
        @"quEvalutionId":self.quEvalutionId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GetDetailBySaved requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                self.detailModel = [ZYCTeachLearningDetailModel yy_modelWithDictionary:responseObject[@"body"]];
                [self.rightButton setTitle:self.detailModel.checkRate forState:UIControlStateNormal];
                self.headerView.dModel = self.detailModel;
                // 重新设置
                [self.tableView layoutIfNeeded];
                self.tableView.tableHeaderView = self.headerView;
                [self.rightButton sizeToFit];
                [self.tableView reloadData];
            }else if([responseObject[@"code"] integerValue] == kContentRevoke){
                self.tableView.hidden = YES;
                self.revokeView.hidden = NO;

            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}



// 提交
- (void)clickRightItem:(UIButton *)sender{
    ZYCTeachingTeachLearnStatisticsViewController *vc = [ZYCTeachingTeachLearnStatisticsViewController new];
    vc.quEvalutionId = self.quEvalutionId;
    vc.titleString = self.detailModel.checkRate;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - getters

- (ZYCMessageRevokeView *)revokeView {
    ZYCWeakSelf
    if (!_revokeView) {
        _revokeView = [[ZYCMessageRevokeView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _revokeView.hidden = YES;
        _revokeView.closeBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        [self.navigationController.view addSubview:_revokeView];
    }
    return _revokeView;
}

- (ZYCTeachingTeachLearningDetailHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZYCTeachingTeachLearningDetailHeaderView alloc] initWithFrame:CGRectZero];
    }
    return _headerView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTeachingTeachLearningDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeachingTeachLearningDetailCell class])];
        [_tableView registerClass:[ZYCTeachingTeachLearningDetailContentCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTeachingTeachLearningDetailContentCell class])];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 144.0f;
    }
    return _tableView;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [[UIButton alloc] init];
        [_rightButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _rightButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 15.0f);
        [_rightButton addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

/*
 
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
